package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.SjProjectMilestone;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.ISjProjectMilestoneService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 项目里程碑Controller
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Controller
@RequestMapping("/sj/milestone")
public class SjProjectMilestoneController extends BaseController
{
    private String prefix = "sj/milestone";

    @Autowired
    private ISjProjectMilestoneService sjProjectMilestoneService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;
    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("gcgl:milestone:view")
    @GetMapping()
    public String milestone(ModelMap mmap)
    {
    	GcProject gcProject = new GcProject();
    	gcProject.setDeleteFlag(1);
    	List<GcProject> polis = gcProjectService.selectGcProjectList(gcProject);
    	mmap.put("polis", polis);
        return prefix + "/milestone";
    }
    
    /**
     * 查询项目里程碑列表
     */
    @RequiresPermissions("gcgl:milestone:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjProjectMilestone sjProjectMilestone)
    {
        startPage();
        sjProjectMilestone.setDeleteFlag(1);
        List<SjProjectMilestone> list = sjProjectMilestoneService.selectSjProjectMilestoneList(sjProjectMilestone);
        
        for(SjProjectMilestone ms:list){
        	GcProject po = gcProjectService.selectGcProjectById(ms.getGcProjectid());
        	ms.setProjectName(po.getName());
        }
        
        return getDataTable(list);
    }
    
    @GetMapping("/milestone/{id}")
    public String milestone(ModelMap mmap,@PathVariable("id") Long id)
    {
    	GcProject gcProject = new GcProject();
    	gcProject.setDeleteFlag(1);
    	gcProject.setId(id);
    	List<GcProject> polis = gcProjectService.selectGcProjectList(gcProject);
    	mmap.put("polis", polis);
    	mmap.put("id", id);
        return prefix + "/milestone4p";
    }
    
    /**
     * 查询项目里程碑列表
     */
    @PostMapping("/list4milestone")
    @ResponseBody
    public TableDataInfo list4milestone(SjProjectMilestone sjProjectMilestone)
    {
        startPage();
        sjProjectMilestone.setDeleteFlag(1);
        sjProjectMilestone.setpId(0l);
        List<SjProjectMilestone> list = sjProjectMilestoneService.selectSjProjectMilestoneList(sjProjectMilestone);
        
        for(SjProjectMilestone ms:list){
        	GcProject po = gcProjectService.selectGcProjectById(ms.getGcProjectid());
        	ms.setProjectName(po.getName());
        }
        
        return getDataTable(list);
    }

    /**
     * 导出项目里程碑列表
     */
    @RequiresPermissions("gcgl:milestone:export")
    @Log(title = "项目里程碑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjProjectMilestone sjProjectMilestone)
    {
    	sjProjectMilestone.setDeleteFlag(1);
        List<SjProjectMilestone> list = sjProjectMilestoneService.selectSjProjectMilestoneList(sjProjectMilestone);
        ExcelUtil<SjProjectMilestone> util = new ExcelUtil<SjProjectMilestone>(SjProjectMilestone.class);
        return util.exportExcel(list, "项目里程碑数据");
    }

    /**
     * 新增项目里程碑
     */
    @GetMapping("/add/{id}")
    public String add(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	SjProjectPeople sjProjectPeople = new SjProjectPeople();
    	sjProjectPeople.setDeleteFlag(1);
    	sjProjectPeople.setGcProjectid(id);
    	List<SjProjectPeople> people = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
    	mmap.put("peoplelis", people);
        return prefix + "/add";
    }
    
    /**
     * 新增项目里程碑
     */
    @GetMapping("/add4Jh/{id}")
    public String add4Jh(ModelMap mmap,@PathVariable("id") Long id){
    	
    	SjProjectMilestone sjProjectMilestone = sjProjectMilestoneService.selectSjProjectMilestoneById(id);
        mmap.put("sjProjectMilestone", sjProjectMilestone);
    	
    	GcProject po = gcProjectService.selectGcProjectById(sjProjectMilestone.getGcProjectid());
    	mmap.put("gcProject", po);
    	
    	SjProjectPeople sjProjectPeople = new SjProjectPeople();
    	sjProjectPeople.setDeleteFlag(1);
    	sjProjectPeople.setGcProjectid(sjProjectMilestone.getGcProjectid());
    	List<SjProjectPeople> people = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
    	mmap.put("peoplelis", people);
        return prefix + "/addJh";
    }

    /**
     * 新增保存项目里程碑
     */
    @RequiresPermissions("gcgl:milestone:add")
    @Log(title = "项目里程碑", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SjProjectMilestone sjProjectMilestone){
    	
    	SysUser sysUser = sysUserService.selectUserById(sjProjectMilestone.getOwnerId());
    	if(sjProjectMilestone.getPstartAt()==null){
    		sjProjectMilestone.setPstartAt(new Date());
    		sjProjectMilestone.setPendAt(new Date());
    		sjProjectMilestone.setSstartAt(new Date());
    		sjProjectMilestone.setSendAt(new Date());
    	};
    	sjProjectMilestone.setOwner(sysUser.getLoginName());
    	sjProjectMilestone.setStatus(0);
    	sjProjectMilestone.setDeleteFlag(1);
    	sjProjectMilestone.setCreateUser(getLoginName());
    	sjProjectMilestone.setCreateAt(new Date());
    	sjProjectMilestone.setUpdateUser(getLoginName());
    	sjProjectMilestone.setUpdateAt(new Date());
        return toAjax(sjProjectMilestoneService.insertSjProjectMilestone(sjProjectMilestone));
    }

    /**
     * 修改项目里程碑
     */
    @RequiresPermissions("gcgl:milestone:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SjProjectMilestone sjProjectMilestone = sjProjectMilestoneService.selectSjProjectMilestoneById(id);
        mmap.put("sjProjectMilestone", sjProjectMilestone);
        
    	GcProject po = gcProjectService.selectGcProjectById(sjProjectMilestone.getGcProjectid());
    	mmap.put("gcProject", po);
    	
    	SjProjectPeople sjProjectPeople = new SjProjectPeople();
    	sjProjectPeople.setDeleteFlag(1);
    	sjProjectPeople.setGcProjectid(sjProjectMilestone.getGcProjectid());
    	List<SjProjectPeople> people = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
    	mmap.put("peoplelis", people);
    	
        return prefix + "/edit";
    }
    
    /**
     * 修改项目里程碑
     */
    @RequiresPermissions("gcgl:milestone:edit")
    @GetMapping("/edit4Jh/{id}")
    public String edit4Jh(@PathVariable("id") Long id, ModelMap mmap)
    {
        SjProjectMilestone sjProjectMilestone = sjProjectMilestoneService.selectSjProjectMilestoneById(id);
        mmap.put("sjProjectMilestone", sjProjectMilestone);
        
    	GcProject po = gcProjectService.selectGcProjectById(sjProjectMilestone.getGcProjectid());
    	mmap.put("gcProject", po);
    	
    	SjProjectPeople sjProjectPeople = new SjProjectPeople();
    	sjProjectPeople.setDeleteFlag(1);
    	sjProjectPeople.setGcProjectid(sjProjectMilestone.getGcProjectid());
    	List<SjProjectPeople> people = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
    	mmap.put("peoplelis", people);
    	
        return prefix + "/editJh";
    }

    /**
     * 修改保存项目里程碑
     */
    @RequiresPermissions("gcgl:milestone:edit")
    @Log(title = "项目里程碑", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SjProjectMilestone sjProjectMilestone)
    {
    	SysUser sysUser = sysUserService.selectUserById(sjProjectMilestone.getOwnerId());
    	
    	if(sjProjectMilestone.getPstartAt()==null){
    		sjProjectMilestone.setPstartAt(new Date());
    		sjProjectMilestone.setPendAt(new Date());
    		sjProjectMilestone.setSstartAt(new Date());
    		sjProjectMilestone.setSendAt(new Date());
    	};
    	sjProjectMilestone.setOwner(sysUser.getLoginName());
    	sjProjectMilestone.setUpdateUser(getLoginName());
    	sjProjectMilestone.setUpdateAt(new Date());
        return toAjax(sjProjectMilestoneService.updateSjProjectMilestone(sjProjectMilestone));
    }

    /**
     * 删除项目里程碑
     */
    @RequiresPermissions("gcgl:milestone:remove")
    @Log(title = "项目里程碑", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjProjectMilestoneService.deleteSjProjectMilestoneByIds(ids));
    }
    
    @GetMapping("/p4milestone/{id}")
    public String p4milestone(ModelMap mmap,@PathVariable("id") Long id)
    {
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailMilestone";
    }

    /**
     * 查询项目里程碑列表
     */
    @PostMapping("/p4list/{gcProjectid}")
    @ResponseBody
    public List<SjProjectMilestone> p4list(SjProjectMilestone sjProjectMilestone,@PathVariable("gcProjectid") Long gcProjectid)
    {
        sjProjectMilestone.setDeleteFlag(1);
        sjProjectMilestone.setGcProjectid(gcProjectid);
        List<SjProjectMilestone> list = sjProjectMilestoneService.selectSjProjectMilestoneList(sjProjectMilestone);
        
        for(SjProjectMilestone ms:list){
        	GcProject po = gcProjectService.selectGcProjectById(ms.getGcProjectid());
        	ms.setProjectName(po.getName());
        }
        
        return list;
    }
    
    @GetMapping("/p4plan/{id}")
    public String p4plan(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	SjProjectMilestone sjProjectMilestone = new SjProjectMilestone();
    	sjProjectMilestone.setGcProjectid(id);
    	sjProjectMilestone.setpId(0l);
    	sjProjectMilestone.setDeleteFlag(1);
        List<SjProjectMilestone> list = sjProjectMilestoneService.selectSjProjectMilestoneList(sjProjectMilestone);
        
        JSONArray jlis = new JSONArray();
        
        for(SjProjectMilestone SjProjectMilestone :list){
        	JSONObject jobj = new JSONObject();
        	JSONArray jlis1 = new JSONArray();
        	jobj.put("id", SjProjectMilestone.getId());
            jobj.put("name", SjProjectMilestone.getName());
            
            SjProjectMilestone sjProjectMilestone1 = new SjProjectMilestone();
        	sjProjectMilestone1.setGcProjectid(id);
        	sjProjectMilestone1.setpId(SjProjectMilestone.getId());
        	sjProjectMilestone1.setDeleteFlag(1);
            List<SjProjectMilestone> list1 = sjProjectMilestoneService.selectSjProjectMilestoneList(sjProjectMilestone1);
            
            if(list1!=null && list1.size()>0){
            	for(SjProjectMilestone so : list1){
                	JSONObject jobj1 = new JSONObject();
                	jobj1.put("name", so.getName());
                	jobj1.put("station", so.getOrderNo());
                	jobj1.put("type", so .getOwner());
                	jobj1.put("start",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, so.getPstartAt()));
                	jobj1.put("end", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, so.getPendAt()));
                	if(so.getStatus()==1){
                		jobj1.put("color", "#65C16F");
                	}else{
                		jobj1.put("color", "#0ea5e7");
                	}
                	
                	jlis1.add(jobj1);
                }
            }else{
            	JSONObject jobj1 = new JSONObject();
            	jobj1.put("name", SjProjectMilestone.getName());
            	jobj1.put("station", SjProjectMilestone.getOrderNo());
            	jobj1.put("type", SjProjectMilestone .getOwner());
            	jobj1.put("start",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, SjProjectMilestone.getPstartAt()));
            	jobj1.put("end", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, SjProjectMilestone.getPendAt()));
            	if(SjProjectMilestone.getStatus()==1){
            		jobj1.put("color", "#65C16F");
            	}else{
            		jobj1.put("color", "#0ea5e7");
            	}
            	
            	jlis1.add(jobj1);
            }
            
            jobj.put("series", jlis1);
            jlis.add(jobj);
        }
        mmap.put("jlis", jlis);
        
        //判断是成员，还是管理员
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
    	
        return "sj/project/projectDetailPlan";
    }
    
    @Log(title = "项目里程碑完成", businessType = BusinessType.UPDATE)
    @PostMapping("/finish")
    @ResponseBody
    public AjaxResult milestoneFinish(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		SjProjectMilestone sjProjectMilestone = new SjProjectMilestone();
    		sjProjectMilestone.setId(Long.parseLong(id[i]));
    		sjProjectMilestone.setStatus(1);
    		sjProjectMilestone.setUpdateUser(getLoginName());
        	sjProjectMilestone.setUpdateAt(new Date());
        	sjProjectMilestoneService.updateSjProjectMilestone(sjProjectMilestone);
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "项目里程碑未完成", businessType = BusinessType.UPDATE)
    @PostMapping("/noFinish")
    @ResponseBody
    public AjaxResult milestoneNoFinish(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		SjProjectMilestone sjProjectMilestone = new SjProjectMilestone();
    		sjProjectMilestone.setId(Long.parseLong(id[i]));
    		sjProjectMilestone.setStatus(0);
    		sjProjectMilestone.setUpdateUser(getLoginName());
        	sjProjectMilestone.setUpdateAt(new Date());
        	sjProjectMilestoneService.updateSjProjectMilestone(sjProjectMilestone);
    	}
    	
    	return toAjax(1);
    }
}